package business.database.sqliteMaescorp

import groovy.sql.Sql
import java.util.HashMap;
import java.util.List;

/**
 * DAO para la tabla word
 * @author Administrador
 *
 */
public class WordDAO {
	
	private def Sql sqlite;
	
	/**
	 * Inicializamos la clase con la instance de SQL
	 * @param sql
	 */
	public WordDAO(Sql sql) {
		sqlite = sql;
	}
	
/**
 * Crea la tabla
 */
	public void create() {
		sqlite.execute("CREATE TABLE IF NOT EXISTS Word (word text COLLATE NOCASE primary key);");
		//sqlite.execute("CREATE INDEX idx_wordorg on Word(word)");
		
		
	}
	
	/**
	 * @return si ya existe la palabra
	 */
	public boolean exists(String word) {
		String sqlQuery = "Select * from Word where word = ?"
		def max = sqlite.firstRow(sqlQuery,[word])
					
		if ((max != null) && (max[0] != null))
			return true;
		else return false;
	}
	
	/**
	 * Inserta una palabra
	 */
	public void insert(String word) {
		sqlite.execute("insert into Word values (?)", [word]);
	}
	

	/**
	 * Obtiene todas las palabras
	 * @return
	 */
	public List<String> selectAll() {
		List<String> words = new ArrayList<String>();
		
		   sqlite.eachRow("select word from Word", {
			  words.add(it[0]); 
		   });
		
		return words;
	}
   
	
	
	
}
